﻿using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UpdateDesktopDocHtmlContent
{
    internal class GlobalConfig
    {
        public const string DataFile = @"D:\Program Files\SendMailServer";
        private static GlobalConfig m_instance;

        public static GlobalConfig Instance
        {
            get
            {
                if(m_instance == null)
                {
                    m_instance = new GlobalConfig();
                    m_instance.Load();
                }
                return m_instance;
            }
        }
        /// <summary>
        /// 本地的IP
        /// </summary>
        public string WebsocketIp { get; set; } = "ws://192.168.2.57:9876";
        /// <summary>
        /// 外网的IP
        /// </summary>
        public string NginxIP { get; set; } = "183.6.116.73:1013";

        public string WebSocketForMdStyle
        {
            get
            {
                return NginxIP;
            }
        }

        public void Save()
        {
            var iniFile = Path.Combine(DataFile, "config.ini");
            File.WriteAllLines(iniFile, new string[]
            {
                "WebsocketIp="+WebsocketIp,//websocketIp,
                "NginxIP="+NginxIP//negix OP
            });
        }

        public void Load()
        {
            var iniFile = Path.Combine(DataFile, "config.ini");
            if (File.Exists(iniFile) == false)
            {
                MessageBox.Show("你需要在" + iniFile + "创建这个文件，里面写WebSocket的地址");
                return;
            }
            var t = File.ReadAllLines(iniFile);
            for (var i = 0; i < t.Length; i++)
            {
                var line = t[i];
                var ti = line.IndexOf(':');
                if (ti == -1)
                {
                    if (i == 0)
                    {
                        WebsocketIp = line;
                    }
                    if (i == 1)
                    {
                        NginxIP = line;
                    }
                    continue;
                }

                var key = line.Substring(0, ti);
                var value = line.Substring(ti + 1);
                if (key == "WebsocketIp") { WebsocketIp = value; }
                if (key == "NginxIP") { NginxIP = value; }
            }
        }
    }
}
